package com.dormitory123.millet.web;

import com.dormitory123.millet.dao.Impl.OrderDaoImpl;
import com.dormitory123.millet.dao.Impl.OrderItemDaoImpl;
import com.dormitory123.millet.dao.OrderDao;
import com.dormitory123.millet.dao.OrderItemDao;
import com.dormitory123.millet.pojo.Order;
import com.dormitory123.millet.pojo.OrderItem;
import com.dormitory123.millet.pojo.User;
import com.dormitory123.millet.service.Impl.OrderServiceImpl;
import com.dormitory123.millet.service.Impl.UserServiceImpl;
import com.dormitory123.millet.service.OrderService;
import com.dormitory123.millet.service.UserService;
import com.dormitory123.millet.util.WebUtils;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author yanhuanzhan
 * @date 2021/12/26 - 19:46
 */
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        //从服务器获取验证码
        String taken= (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //输出Session中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
         //1,验证码验证
        if (taken!=null&&taken.equalsIgnoreCase(code)) {//验证成功
            if (userService.existUsername(username)) {//用户名存在
                User loginUser=userService.login(username, password);
                if ( loginUser!= null) {//登录成功
                    ////获取订单信息
                    List<Order> orders = orderService.queryOrdersByUserId(loginUser.getId());
                        //System.out.println(orders);
                    //if (orders.size() != 0) {
                    //    Map<String, List<OrderItem>> orderItemMap = orderService.queryOrderItems(orders);
                    //    System.out.println(orderItemMap);
                    //         //保存订单项map
                    //    req.getSession().setAttribute("orderItemMap",orderItemMap);
                    //}
                    ////保存订单集合
                    //req.getSession().setAttribute("orders",orders);
                    //
                    //保存订单个数
                    req.getSession().setAttribute("orderCount",orders.size());
                    req.setAttribute("msg","");
                    //保存用户登录信息
                    req.getSession().setAttribute("user",loginUser);
                    req.getRequestDispatcher("/index.jsp").forward(req,resp);
                }else {
                    req.setAttribute("msg","用户名或密码错误!");
                    System.out.println("用户名或密码错误!");
                    req.setAttribute("username",username);
                    req.setAttribute("password",password);
                    req.getRequestDispatcher("/login.jsp").forward(req,resp);
                }
            }else {//用户名不存在
                req.setAttribute("msg","用户名不存在!");
                System.out.println("用户名不存在!");
                req.setAttribute("username",username);
                req.setAttribute("password",password);
                req.getRequestDispatcher("/login.jsp").forward(req,resp);
            }
        }else {//验证失败
            req.setAttribute("msg","验证码错误!");
            System.out.println("验证码错误!");
            req.setAttribute("username",username);
            req.setAttribute("password",password);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

    }
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = WebUtils.copyParamToBean(req.getParameterMap(), new User());
        if (userService.existUsername(user.getUsername())) {//用户名存在
            //把错误信息和回显信息保存
            req.setAttribute("msg","用户名已经存在!");
            req.setAttribute("username", user.getUsername());
            req.setAttribute("password", user.getPassword());
            req.setAttribute("phone", user.getPhone());
            req.setAttribute("email",user.getEmail());
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        }else {
           userService.regist(user);
           req.setAttribute("msg","");
           resp.sendRedirect("/Millet/login.jsp");
        }

    }
    protected void cancell(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().setAttribute("user",null);
         resp.sendRedirect(req.getHeader("Referer"));
    }
}
